<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\Pay;
use app\model\Config;
use app\model\Puddle;
use app\model\PuddleBanner;
use app\model\PuddleDetail;
use app\model\PuddleOrders;
use app\model\User;
use app\tool\Random;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use think\db\Query;
use think\facade\Db;

class PuddleController extends BaseController
{
	
	function profile()
	{
		$business_hours = Config::getFieldByName('business_hours', 'value');
		$puddle_content = Config::getFieldByName('puddle_content', 'value');
		$banner = PuddleBanner::order(['weigh' => 'desc'])->select();
		$this->writeJson([
			'business_hours' => $business_hours,
			'puddle_content' => $puddle_content,
			'banner' => $banner,
		]);
	}
	
	function index()
	{
		$rows = Puddle::withCount(['detail' => function (Query $query, &$alias) {
			$alias = 'count';
		}])
			->filter(function ($data) {
				$data->use_count = $data->orders()->where('day', date('Y-m-d'))->whereIn('pay_status', [2, 3])->count();
			})
			->order(['weigh' => 'desc'])->select();
		$this->writeJson($rows);
	}
	
	function detail()
	{
		$puddle_id = $this->request->get('puddle_id/d');
		$day = $this->request->get('day');
		if (!$day || !$puddle_id) {
			$this->writeJson(0, '参数错误');
		}
		$uservippuddle = User::where(['level' => 3, ['puddle_detail_id', '<>', 0]])->column('puddle_detail_id');
		$img = Puddle::where(['id' => $puddle_id])->value('img');
		$rows = PuddleDetail::where(['puddle_id' => $puddle_id])
			->filter(function ($data) use ($day,$uservippuddle) {
				$row = $data->orders()->where(['day' => $day])->whereIn('pay_status', [2, 3])->find();
				if ($row) {
					$data->use = 1;
				} else {
					if (in_array($data->id, $uservippuddle)) {
						$data->use = 1;
					} else {
						$data->use = 0;
					}
				}
			})->order(['weigh' => 'desc'])->select();
		$this->writeJson(['img'=>$img,'list'=>$rows]);
	}
	
	#创建订单
	function createOrder()
	{
		$detail_id = $this->request->post('detail_id');
		$day = $this->request->post('day');
		$pay_type = $this->request->post('pay_type/d'); //支付方式
		$row = PuddleDetail::findOrFail($detail_id);
		$user = User::findOrFail($this->user_id);
		if ($user->level != 2 && $row->type == 2) {
			$this->writeJson(101, '您还未开通Vip会员,无法预约钓位');
		}
		$detail = PuddleOrders::where(['day' => $day,'detail_id'=>$detail_id])->whereIn('pay_status', [2, 3])->find();
		if ($detail) {
			$this->writeJson(102, '该位置已被人预定');
		}
		$business_hours = Config::getFieldByName('business_hours', 'value');//营业时间
		$ordersn = Random::ordersn();
		$off_sn = Random::ordersn();
		$pay_amount = $row->price;
		PuddleOrders::create([
			'user_id' => $this->user_id,
			'detail_id' => $row->id,
			'pay_amount' => $pay_amount,
			'order_sn' => $ordersn,
			'off_sn' => $off_sn,
			'day' => $day,
			'business_hours' => $business_hours
		]);
		
		// 启动事务
		Db::startTrans();
		try {
			$ret = Pay::pay($pay_type, $pay_amount, $ordersn, '预定钓位', 'puddle', $user->openid);
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			$this->writeJson(0, $e->getMessage());
		}
		$this->writeJson($ret);
	}
	
	#vip钓位
	function vipPuddle()
	{
		$uservippuddle = User::where(['level' => 3, ['puddle_detail_id', '<>', 0]])->column('puddle_detail_id');
		$rows = PuddleDetail::where(['type' => 2])->filter(function ($data) use ($uservippuddle) {
			$row = $data->orders()->whereTime('day', '>=', date('Y-m-d'))->whereIn('pay_status', [2, 3])->find();
			if ($row) {
				$data->use = 1;
			} else {
				if (in_array($data->id, $uservippuddle)) {
					$data->use = 1;
				} else {
					$data->use = 0;
				}
			}
		})->order(['weigh' => 'desc'])->select();
		$this->writeJson($rows);
	}
	
	#订单列表
	function orderList()
	{
		$status = $this->request->get('status/d'); // 0全部 1待核销 2完成 3过期
		$rows = PuddleOrders::with(['detail' => function (Query $query) {
			$query->with(['puddle']);
		}])->where(['user_id' => $this->user_id, ['pay_status', '<>', 1]])->when(!empty($status), function (Query $query) use ($status) {
			if ($status == 1) {
				$query->where(['pay_status' => 2]);
			}
			if ($status == 2) {
				$query->where(['pay_status' => 3]);
			}
			if ($status == 3) {
				$query->where(['pay_status' => 4]);
			}
		})->order(['id' => 'desc'])->paginate()->items();
		$this->writeJson($rows);
	}
	
	function orderDetail()
	{
		$order_id = $this->request->get('order_id/d');
		$row = PuddleOrders::where(['user_id' => $this->user_id])
			->with(['detail' => function (Query $query) {
				$query->with(['puddle']);
			}])
			->filter(function ($data) {
				if ($data->pay_status == 2) {
					
					
					$writer = new PngWriter();
					$qrCode = QrCode::create(json_encode(['action'=>'puddle_order_off','order_sn'=>$data->order_sn]))//跳转的url地址
					->setSize(100)      //大小
					->setMargin(0)             //边距
					->setForegroundColor(new Color(0, 0, 0))        //前景色
					->setBackgroundColor(new Color(255, 255, 255));       //背景色
					$result = $writer->write($qrCode)->getDataUri();
					$data->qrdata = $result;
				}
				
			})
			->findOrFail($order_id);
		$this->writeJson($row);
	}
	
	
	
	
}
